package com.hankers.localvideoplayer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import android.content.Context;
import android.os.Environment;

public class MyConfig {

	public final static String PROJECT_DIR = "adplayer";
	
	final static String CONFIG_FILE = "server.config";

	Context _context;
	String _projectDir;
	
	public MyConfig(Context context) {

		_context = context;
		_projectDir = null;
		_exitPWD = context.getResources().getString(R.string.exit_pwd);

		loadSettingsFromExternal();
	}

	String _exitPWD = "";

	public String getExitPWD() {
		return _exitPWD;
	}

	public void setExitPWD(String exitPWD) {
		_exitPWD = exitPWD;
	}

	void loadSettingsFromExternal() {

		Properties prop = new Properties();
		InputStream input = null;
		OutputStream output = null;

		try {
			String filepath = getFileAbsolutePath(CONFIG_FILE);

			File f = new File(filepath);
			if (f.exists()) {
				input = new FileInputStream(filepath);

				// load a properties file
				prop.load(input);

				_exitPWD = prop.getProperty("exit_pwd");
			} else {
				output = new FileOutputStream(filepath);

				// set the properties value
				prop.setProperty("exit_pwd", _exitPWD);

				// save properties to project root folder
				prop.store(output, null);
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
					input = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (output != null) {
				try {
					output.close();
					output = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	int getSafeInt(String src, int defaultValue) {
		try {
			
			if(src != null && src.length() > 0)
				return Integer.parseInt(src);
			
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return defaultValue;
	}
	
	public String getProjectDir() {
		
		if(_projectDir == null) {
			String parentPath = Environment.getExternalStorageDirectory().getParent();
			
			File parentDir = new File(parentPath);
			
			if(parentDir != null && parentDir.isDirectory()) {
				File[] subfiles = parentDir.listFiles();
				
				for(int i = subfiles.length - 1; i >= 0; --i) {
					File subf = subfiles[i];
					if(subf.isDirectory()) {
						
						String folder = subf.getAbsolutePath();
						String project_dir = folder + "/" + PROJECT_DIR + "/";
						if((new File(project_dir)).isDirectory()) {
							_projectDir = project_dir;
							break;
						}
					}
				}
			}
		}
		
		if(_projectDir == null) {
			String folder = Environment.getExternalStorageDirectory().getAbsolutePath();
			_projectDir = folder + "/" + PROJECT_DIR + "/";
		}
		
		return _projectDir;
	}

	String getFileAbsolutePath(String filename) {
		
		return getProjectDir() + filename;
	}
}
